/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ueb16;

/**
 *
 * @author steffenschuhmann
 */
public abstract class BTreeNodeImp implements BTreeNode, Comparable<BTreeNodeImp> {
    
    protected BTreeNode[]     childField;
    protected DataObject[]    dataField;
    protected int numChild;
    protected int numData;
    
    public BTreeNodeImp(int order) throws BTreeException {
        if(order < 2)
            throw new BTreeException();
        
        childField  = new BTreeNode[order];
        dataField   = new DataObject[order - 1];
        numChild    = 0;
        numData     = 0;
    }

    public BTreeNode getChildField(int position) throws BTreeException {
        if(position >= childField.length)
            throw new BTreeException();
        
        if(childField[position] != null)
            return childField[position];
        
        return null;
    }

    public void addChild(BTreeNodeImp child) {
        if (numChild == childField.length)
            throw new NullPointerException();
        
        childField[numChild] = child;
        numChild++;
    }
    
    public boolean isFull() {
        if (numData < dataField.length)
            return false;
        return true;
    }

    public DataObject getDataAt(int position) throws BTreeException {
        if(position >= dataField.length)
            throw new BTreeException();
        
        return dataField[position];
    }

    public void setDataField(DataObject[] dataField) {
        this.dataField = dataField;
    }

    public int getNumChild() {
        return numChild;
    }

    public int getNumData() {
        return numData;
    }
    
    
    
    public String toString() {
        throw new NullPointerException();
    }

    @Override
    public int compareTo(BTreeNodeImp t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isLeaf() {
        return (childField[0]==null) ? true : false;
    }
    
}
